package com.yiqi.liebang.feature.news.view;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.suozhang.framework.component.http.RxDataProcessFactory;
import com.suozhang.framework.component.recyclerview.DividerItemDecoration;
import com.suozhang.framework.framework.AM;
import com.suozhang.framework.framework.BaseActivity;
import com.suozhang.framework.framework.Callback;
import com.suozhang.framework.utils.T;
import com.suozhang.framework.widget.EmptyView;
import com.yiqi.liebang.R;
import com.yiqi.liebang.entity.bo.IssuesBo;
import com.yiqi.liebang.entity.bo.SearchFriendBo;
import com.yiqi.liebang.feature.home.view.adapter.FriendsAdapter;
import com.yiqi.liebang.feature.mine.view.UserInfoActivity;
import com.yiqi.liebang.feature.news.contract.IssuesContract;
import com.yiqi.liebang.feature.news.dependencies.issues.DaggerIssuesComponent;
import com.yiqi.liebang.feature.news.dependencies.issues.IssuesPresenterModule;
import com.yiqi.liebang.feature.news.view.adapter.PendingIssuesAdapter;
import com.yiqi.liebang.feature.people.view.PeopleActivity;
import com.yiqi.liebang.framework.api.HomeApi;

import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.rong.imkit.RongIM;
import io.rong.imlib.IRongCallback;
import io.rong.imlib.RongIMClient;
import io.rong.imlib.model.Conversation;
import io.rong.imlib.model.Message;
import io.rong.message.TextMessage;

public class PendingIssuesActivity extends BaseActivity implements IssuesContract.View, BaseQuickAdapter.OnItemChildClickListener, OnRefreshListener, BaseQuickAdapter.OnItemClickListener {


    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.rv_unread_msg)
    RecyclerView mRvUnreadMsg;
    @BindView(R.id.smartRefesh)
    SmartRefreshLayout mSmartRefesh;
    PendingIssuesAdapter mAdapter;
    @BindView(R.id.rv_hytj)
    RecyclerView mRvHytj;
    private EmptyView mEmptyView;

    private EmptyView mEmptyViewFriend;
    private int pos;

    @Inject
    IssuesContract.Presenter mPresenter;
    private FriendsAdapter questionAdapter;
    private String mTargetId;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_pending_issues;
    }

    @Override
    protected void initInjector() {
        DaggerIssuesComponent.builder().issuesPresenterModule(new IssuesPresenterModule(this)).build().inject(this);
    }

    @Override
    protected void initView() {
        initToolBar(mToolbar, "待处理事项", true, true);
        initAdapter();
        initFriendAdapter();
        getData();
    }

    @Override
    protected void initData() {
        mPresenter.getIssuesBo();



    }

    private void initAdapter() {
        mEmptyView = new EmptyView(mRvUnreadMsg);
        mRvUnreadMsg.setLayoutManager(new LinearLayoutManager(this));
        mRvUnreadMsg.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        mAdapter = new PendingIssuesAdapter();
        mAdapter.bindToRecyclerView(mRvUnreadMsg);
        mAdapter.setOnItemChildClickListener(this);
        mSmartRefesh.setOnRefreshListener(this);
        mSmartRefesh.setEnableLoadMore(false);
    }

    @Override
    public void showIssuesSuccess(List<IssuesBo> s) {
        mSmartRefesh.finishRefresh();
        mAdapter.setNewData(s);
//        getData();
    }

    @Override
    public void showRefuseFriendSuccess() {
        showMsg("拒绝成功！");
        mSmartRefesh.autoRefresh();
    }

    @Override
    public void showPassFriendSuccess() {
        showMsg("通过请求成功！");
        mSmartRefesh.autoRefresh();
        TextMessage mTextMessage = TextMessage.obtain("我已通过了好友请求，以后多多交流");
        RongIM.getInstance().sendMessage(Message.obtain(mTargetId, Conversation.ConversationType.PRIVATE, mTextMessage), null, null,
                new IRongCallback.ISendMessageCallback() {
                    @Override
                    public void onAttached(Message message) {

                    }

                    @Override
                    public void onSuccess(Message message) {

                    }

                    @Override
                    public void onError(Message message, RongIMClient.ErrorCode errorCode) {

                    }
                });
    }

    @Override
    public void showEmpty() {
        mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getEmptyView());
        mAdapter.setNewData(null);
    }

    @Override
    public void showIssuesError(String error) {
        mSmartRefesh.finishRefresh();
        mAdapter.setEmptyView(mEmptyView.getErrorView(error));
        mAdapter.setNewData(null);
    }

    @Override
    public void showError(String error) {
        T.showShort(error);
    }

    @Override
    public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
        pos = position;
        switch (view.getId()) {
            case R.id.btn_refused_add:
                mPresenter.getRefuseFriend(mAdapter.getData().get(position).getId());
                break;
            case R.id.btn_pass_add:
                mPresenter.getPassFriend(mAdapter.getData().get(position).getId());
                mTargetId = mAdapter.getData().get(position).getUserAccountFormMap().getUserUid();
                break;

            case R.id.iv_friend_head:
                Intent intent = new Intent(this, UserInfoActivity.class);
                intent.putExtra("is_view_my", false);
                intent.putExtra("visitorUid", mAdapter.getData().get(position).getUserAccountFormMap().getUserUid());
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        initData();
    }

    private void getData() {
        AM.api().createApiService(HomeApi.class).tuijianFriends()
                .compose(RxDataProcessFactory.dataPrepAndIoToMainTransformer())
                .subscribe(new Observer<List<SearchFriendBo.UserFriendBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(List<SearchFriendBo.UserFriendBean> friendBeans) {
                        if (friendBeans != null && friendBeans.size() > 0) {
                            questionAdapter.setNewData(friendBeans);
                        } else {
                            questionAdapter.setEmptyView(mEmptyViewFriend.getEmptyView("暂无推荐好友"));
                            questionAdapter.setNewData(null);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        questionAdapter.setEmptyView(mEmptyViewFriend.getErrorView(e.getMessage()));
                        questionAdapter.setNewData(null);
                    }

                    @Override
                    public void onComplete() {
                        dismissLoading();
                    }
                });
    }

    private void initFriendAdapter() {
        mEmptyViewFriend = new EmptyView(mRvHytj);
        mRvHytj.setLayoutManager(new LinearLayoutManager(this));
        mRvHytj.addItemDecoration(new DividerItemDecoration(DividerItemDecoration.VERTICAL).setHeight(2));
        questionAdapter = new FriendsAdapter(true, this);
        questionAdapter.bindToRecyclerView(mRvHytj);
        questionAdapter.setOnItemClickListener(this);
        questionAdapter.setCommonCallback(new Callback.CommonCallback() {
            @Override
            public void onSubscribe(Disposable d) {
                showLoading(d);
            }

            @Override
            public void onSuccess(Object result) {
                dismissLoading();
            }

            @Override
            public void onError(Throwable ex) {
                dismissLoading();
            }

            @Override
            public void onComplete() {
                dismissLoading();
            }
        });

    }

    @OnClick(R.id.btn_more_tj)
    public void onViewClicked() {
        Intent intent = new Intent(this, PeopleActivity.class);
        startActivity(intent);
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        Intent intent = new Intent(this, UserInfoActivity.class);
        intent.putExtra("is_view_my", false);
        intent.putExtra("visitorUid", questionAdapter.getData().get(position).getUserUid());
        startActivity(intent);
    }
}
